package com.kfm.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter(servletNames = {"com.kfm.servlet.CaptchaServlet"}, dispatcherTypes = {DispatcherType.FORWARD, DispatcherType.REQUEST})
public class CaptchaFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("CaptchaFilter.doFilter  start");

        String captcha = servletRequest.getParameter("captcha");
        if (captcha != null){
            // 放行
            filterChain.doFilter(servletRequest, servletResponse);
        } else {
            HttpServletResponse servletResponse1 = (HttpServletResponse) servletResponse;
            servletResponse1.sendError(406, "验证码不正确");
        }

        System.out.println("CaptchaFilter.doFilter  end");
    }
}
